{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pytorch - version\n",
    "#\n",
    "'''\n",
    "https://www.w3cschool.cn/pytorch\n",
    "\n",
    "PyTorch 是一个 Torch7 团队开源的 Python 优先的深度学习框架，提供两个高级功能：\n",
    "1. 强大的 GPU 加速 Tensor 计算(类似 numpy)\n",
    "2. 构建基于 tape 的自动升级系统上的深度神经网络\n",
    "\n",
    "在粒度级别上,PyTorch 是一个由以下组件组成的库：\n",
    "torch\n",
    "torch.autograd\n",
    "torch.nn\n",
    "torch.optim\n",
    "torch.multiprocessing\n",
    "torch.utils\n",
    "torch.legacy\n",
    "\n",
    "通常使用 PyTorch 是将其作为：\n",
    "作为 numpy 的替代品，以使用强大的 GPU 能力；\n",
    "一个深度学习研究平台，提供最大的灵活性和速度。\n",
    "'''\n",
    "import torch\n",
    "\n",
    "print(torch.__version__)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pytorch - device\n",
    "#\n",
    "import torch\n",
    "\n",
    "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
    "print(device)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pytorch - tensor\n",
    "#\n",
    "import torch\n",
    "import numpy as np\n",
    "\n",
    "# 创建一个没有初始化的 5 * 3 矩阵 - tensor\n",
    "t = torch.empty(2, 3)   # torch.ones torch.zeros\n",
    "print(t)\n",
    "print(t.size())\n",
    "print()\n",
    "\n",
    "# tensor --> numpy\n",
    "print('tensor --> numpy')\n",
    "n = t.numpy()\n",
    "print(n)\n",
    "print()\n",
    "\n",
    "# numpy --> tensor\n",
    "print('numpy --> tensor')\n",
    "n = np.ones([3, 2])\n",
    "t = torch.from_numpy(n)\n",
    "print(t)\n",
    "print()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# pytorch - tensor on CUDA\n",
    "#\n",
    "import torch\n",
    "\n",
    "x0 = torch.empty(2, 3)\n",
    "x0 = torch.zeros(2, 3)\n",
    "print(x0)\n",
    "\n",
    "if torch.cuda.is_available():\n",
    "    _device = torch.device(\"cuda\")          # a CUDA device object\n",
    "    # print(_device)\n",
    "    y = torch.ones_like(x0, device=_device)  # 直接在GPU上创建tensor\n",
    "    print(y)\n",
    "    x = x0.to(device)                        # 或者使用`.to(\"cuda\")`方法\n",
    "    print(x)\n",
    "    z = x+y\n",
    "    print(z)\n",
    "    print((z+1).to(\"cpu\", torch.double))       # `.to`也能在移动时改变dtype\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.9674, -0.8887, -0.4882, -2.4967],\n",
      "        [ 0.1988, -0.3475,  0.7723,  0.3385],\n",
      "        [-0.3050,  1.6980, -0.4515, -0.9136],\n",
      "        [ 0.7756, -0.1953, -0.1993,  0.3100]])\n",
      "torch.Size([4, 4])\n",
      "\n",
      "tensor([-0.9674, -0.8887, -0.4882, -2.4967,  0.1988, -0.3475,  0.7723,  0.3385,\n",
      "        -0.3050,  1.6980, -0.4515, -0.9136,  0.7756, -0.1953, -0.1993,  0.3100])\n",
      "torch.Size([16])\n",
      "\n",
      "tensor([[-0.9674, -0.8887, -0.4882, -2.4967,  0.1988, -0.3475,  0.7723,  0.3385],\n",
      "        [-0.3050,  1.6980, -0.4515, -0.9136,  0.7756, -0.1953, -0.1993,  0.3100]])\n",
      "torch.Size([2, 8])\n",
      "\n"
     ]
    }
   ],
   "source": [
    "# pytorch - view\n",
    "#\n",
    "# 改变形状\n",
    "#\n",
    "import torch\n",
    "\n",
    "x = torch.randn(4, 4)\n",
    "print(x)\n",
    "print(x.size())\n",
    "print()\n",
    "\n",
    "y = x.view(16)\n",
    "print(y)\n",
    "print(y.size())\n",
    "print()\n",
    "\n",
    "z = x.view(-1, 8)  # the size -1 is inferred(推断) from other dimensions\n",
    "print(z)\n",
    "print(z.size())\n",
    "print()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.7"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "60d0b561bcf179bd0cd76361af4313e00281c3c5daca1903769e6cc3dd82b37c"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
